package pcloud.org.server;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.View;

import pcloud.org.server.resource.FilesInfoResource;

public class ResourceErrorView implements View
{
	private static final Log log = Logs.getLog(FilesInfoResource.class);

	public void render(HttpServletRequest req, HttpServletResponse resp, Object obj)
	{
		if (obj.getClass().isAssignableFrom(FileNotFoundException.class)) {
			resp.setStatus(404);
			Writer writer;
			try {
				writer = resp.getWriter();
				writer.write("File resouce doesn't exist!");
				writer.close();
			} catch (IOException ioe) {
				log.error("write response failed when handling exception \r\n!" + ioe.getMessage());
			}
		} else if (obj.getClass().isAssignableFrom(Exception.class)) {
			Exception e = (Exception) obj;

			resp.setStatus(500);
			Writer writer;
			try {
				writer = resp.getWriter();
				writer.write(e.getMessage());
				writer.close();
			} catch (IOException ioe) {
				log.error("write response failed when handling exception \r\n!" + ioe.getMessage());
			}
		} else {
			resp.setStatus(500);
			Writer writer;
			try {
				writer = resp.getWriter();
				writer.write("Unknown error");
				writer.close();
			} catch (IOException e) {
				
			}
		}
	}
}
